<template>
  <div class="z-select" ref="zselect"  :view-id="propcheck">
    <!-- <input type="text" v-bind:value="checked" v-on:change="$emit('change',curcheck.id)"> -->
    <el-input class="z-input" readonly v-if="!multiple" size="mini" suffix-icon="el-icon-arrow-up" :class="{'z-show':showTree}" v-model="curcheck[childprops.name]" @focus='foucsOption'></el-input>
    <el-input class="z-input" readonly v-if="multiple" size="mini" suffix-icon="el-icon-arrow-up"  :class="{'z-show':showTree}" v-model="arrtext" @focus='foucsOption'></el-input>
    <div class="z-dropdown" v-show="showTree">
      <div class="z-options">
        <ztree :defaults="curchecked" :names="namearrs" :arrdefaults='arrcurchecked' v-on:namearr="namearr" :multiple="multiple" :datas="treedata[0]" :childprops="childprops" :showLv='2' v-on:curcheck="curCheck"></ztree>
      </div>
      <div class="z-save">
        <el-button size="mini" @click="selectSave">确定</el-button>
      </div>

    </div>

  </div>

</template>

<script>
import ztree from "./tree.vue";
export default {
  name: "ztree-select",
  components: {
    ztree
  },
  model: {
    prop: "checked",
    event: "change"
  },
  props: {
    checked: {},
    multiple: Boolean,
    treedata:{
      type:Array
    },
    childprops:{
      type:Object,
      default: {
        name: "displayName",
        children: "orgs",
        lv:'depth'
      }
    }
  },
  data() {
    return {
      
      select: false,
      curcheck: {}, //单选
      showTree: false,
      arr: [], //多选
      arrtext:'',
      curchecked:'',
      arrcurchecked:[],
      namearrs:[]
    };
  },
  computed: {
    propcheck(){
      if(this.multiple){
       
        this.arrcurchecked = this.checked;
        if(this.checked.length >0){
          this.arrtext =  '已选择'+this.checked.length+'项'
        }else{
           this.arrtext =  '请选择'
        }
        
      }else{
         this.curchecked = this.checked;


      }
    }
  },
  created() {
    if (this.checked) {
    }
    document.addEventListener("click", e => {
      if (!this.$refs.zselect.contains(e.target)) {
        this.showTree = false;
      }
    });
  },
  methods: {
    namearr(val){//提交name
    console.log(val)
      this.$emit("namearr",val)
    },
    curCheck(val) {
   
      if (this.multiple) {
        this.arr = val;
        let arr = this.uq(val)
        this.$emit("change", arr);
      } else {
        this.curcheck = val;
        this.curchecked = val.id;
        this.$emit("change", val.id);
      }
    },
    //排序
    uq(array){
      function sorts(a,b){
        return a-b
      }
      array.sort(sorts);
      return array
    },
    selectSave() {
      //let data = this.curcheck;
      this.showTree = false;
    },
    foucsOption() {
      this.showTree = true;
    },
    handleClose() {
    }
  }
};
</script>

<style lang="scss" scoped>
.z-select {
  display: inline-block;
  position: relative;
}
.z-dropdown {
  padding-bottom: 40px;
  position: absolute;
  width: 100%;
  height: 280px;
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  box-sizing: border-box;
  z-index: 81111;
  transition: all ease 0.5s;
}
.z-options {
  width: 100%;
  height: 237px;
  overflow-y: auto;
  position: absolute;
  z-index: 1001;
  border-bottom: 1px solid #e4e7ed;
}
.z-save {
  position: absolute;
  left: 0;
  bottom: 5px;
  text-align: center;
  width: 100%;
}

</style>
<style>
.z-input .el-input__inner{
    cursor: pointer;
 
}
.el-input__suffix{
  transform: rotate(180deg)
}
.z-show .el-input__suffix{
  transform: rotate(0)
}
</style>

